<template>
  <div class="main" v-if="isShow" @click.self="cancel">
    <div class="main-container ">
      <!-- 关闭按钮 -->
      <div class="close-btn">
        <el-icon color="#fe455d" size="25px" @click="cancel()">
          <CircleCloseFilled />
        </el-icon>
      </div>

      <!-- 左边区域 -->
      <div class="left">
        <!-- 头像 -->
        <div class="avatar">
          <img :src="userInfo.avatar" alt=""
            style="width: 250px; height: 250px; border-radius: 50%; object-fit: cover;">
        </div>
        <!-- 上传头像 -->
        <div class="fileSelector">
          <el-upload :on-change="(file: UploadFile, newFileList: UploadFile[]) => handleChange(file)"
            :auto-upload="false" :limit="1" :show-file-list="false" :file-list="avatarFile">
            <template #trigger>
              <el-button class="upload-btn" plain>
                <svg class="upload-icon" aria-hidden="true">
                  <use xlink:href="#icon-shangchuantouxiang-copy"></use>
                </svg>
                <span>更换头像</span>
              </el-button>
            </template>
          </el-upload>
        </div>
      </div>

      <!-- 右边区域 -->
      <div class="right">
        <!-- 个人信息 -->
        <el-form ref="userInfoFormRef" style="max-width: 550px" status-icon label-width="70" class="From">
          <el-form-item label="昵称:" prop="nickName" label-width="70px">
            <el-input class="name-input" v-model="userInfo.nickName" type="text" style="width: 200px" clearable />
          </el-form-item>
          <el-form-item label="性别:" prop="sex" label-width="70px">
            <el-radio-group v-model="userInfo.sex">
              <el-radio value="1" border fill="#FF3C55" text-color="#FF3C55">
                男
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-nanhai"></use>
                </svg>
              </el-radio>
              <el-radio value="0" border>
                女
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-nvhai"></use>
                </svg>
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="省份:" prop="province" label-width="70px">
            <el-select v-model="userInfo.province" style="width: 100px" placeholder="选择省份">
              <el-option v-for="item in regionData" :key="item.value" :label="item.label" :value="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="城市:" prop="city" label-width="70px">
            <el-select v-model="userInfo.city" style="width: 100px" placeholder="选择城市">
              <el-option v-for="item in cityList" :key="item.value" :label="item.label" :value="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="介绍:" prop="intro" label-width="70px">
            <el-input class="intro-input" v-model="userInfo.intro" resize="none" :rows="4" type="textarea"
              show-word-limit maxlength="50" placeholder="作个自我介绍吧~" />
          </el-form-item>
          <el-form-item>
            <el-button class="cancel-btn" @click="cancel()">取消</el-button>
            <el-button @click="save()">保存</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { CircleCloseFilled } from '@element-plus/icons-vue'
import { onMounted, ref, watch } from 'vue'
import { useUserStore } from '@/stores'
import { regionData } from 'element-china-area-data'
import 'animate.css'

onMounted(() => {
  // 获取用户信息
  userInfo.value = { ...userStore.user }
})

const userStore = useUserStore()
// 是否展示登陆框
const isShow = ref(false)
// 展示登陆框
const show = () => {
  isShow.value = true
}

// 取消
const cancel = () => {
  // 获取用户信息
  userInfo.value = { ...userStore.user }
  isShow.value = false
}
// 保存
const save = () => {
  // 保存用户信息
  saveUserInfo().then(() => {

    isShow.value = false
  })
}
// 暴露给外部的方法
defineExpose({
  show,
})

// 相关的数据和逻辑方法
import useBasicInfo from '@/hooks/useBasicInfo'
import type { UploadFile } from 'element-plus';
const { userInfo, cityList, avatarFile, userInfoFormRef, resetCity, handleChange, saveUserInfo } = useBasicInfo()

// 监视userInfo的省份变化
watch(userInfo, () => {
  resetCity()
}, { deep: true })
</script>
<style lang="less" scoped>
.main {
  width: 100vw;
  height: 100vh;
  background-color: var(--mask-background-color);
  backdrop-filter: blur(3px);
  position: fixed;
  left: 0;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  pointer-events: all;
  z-index: 999;
  transition: all 0.3s ease;

  .main-container {
    width: 800px;
    height: 500px;
    background-color: var(--background-color);
    position: relative;
    border-radius: 20px;
    display: flex;
    align-items: stretch;
    overflow: hidden;
    transition: transform 0.3s ease;

    .close-btn {
      position: absolute;
      right: 20px;
      top: 20px;
      cursor: pointer;
      transition: all 0.3s ease;
      z-index: 10;
      // opacity: 0.9;

      &:hover {
        transform: scale(1.1);
        opacity: 1;
      }
    }

    .left {
      width: 40%;
      padding: 40px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      border-right: 1px solid rgba(0, 0, 0, 0.1);
      background-color: var(--dark-background-color);

      .avatar {
        margin-bottom: 20px;

        img {
          width: 180px;
          height: 180px;
          border-radius: 50%;
          object-fit: cover;
          box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
          transition: all 0.3s ease;

          &:hover {
            transform: scale(1.02);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
          }
        }
      }

      .fileSelector {
        margin-top: 20px;
        width: 100%;
        display: flex;
        justify-content: center;

        :deep(.el-upload) {
          width: 100%;
          display: flex;
          justify-content: center;
        }

        .upload-btn {
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 8px;
          width: 120px;
          height: 36px;
          background: transparent;
          color: var(--theme-color);
          border: 2px solid var(--theme-color);
          font-size: 14px;
          border-radius: 25px;
          font-weight: 600;
          cursor: pointer;
          transition: all 0.3s ease;

          &:hover {
            background: var(--theme-color);
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(var(--theme-color-rgb), 0.3);

            .upload-icon {
              fill: white;
            }
          }

          .upload-icon {
            width: 1.2em;
            height: 1.2em;
            fill: var(--theme-color);
            transition: all 0.3s ease;
          }

          span {
            display: inline-block;
            margin-top: 1px;
          }
        }
      }
    }

    .right {
      width: 60%;
      padding: 40px;
      display: flex;
      flex-direction: column;
      justify-content: center;

      .From {
        width: 100%;
        max-width: 380px;
        margin: 0 auto;
      }

      .el-form-item {
        margin-bottom: 15px;
      }

      .name-input {
        :deep(.el-input__wrapper) {
          background-color: var(--dark-background-color);
          border-radius: 25px;
          transition: all 0.3s ease;
        }
      }

      .intro-input {
        width: 100%;

        :deep(.el-textarea__inner) {
          border-radius: 15px;
          padding: 12px;
          background-color: var(--dark-background-color);
          resize: none;
          transition: all 0.3s ease;

          &:hover,
          &:focus {
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
          }
        }

        // 美化字数限制样式
        :deep(.el-input__count) {
          background: transparent;
          color: #909399;
          font-size: 12px;
          position: absolute;
          bottom: 5px;
          right: 10px;

          &::after {
            content: '';
            position: absolute;
            left: -20px;
            top: 50%;
            transform: translateY(-50%);
            width: 1px;
            height: 14px;
            background-color: #dcdfe6;
            opacity: 0.5;
          }

          .el-input__count-inner {
            background: transparent;
            padding: 0 5px;

            // 当接近字数限制时的样式
            &.is-exceed {
              color: var(--el-color-danger);

              span {
                color: var(--el-color-danger);
              }
            }
          }
        }

        // 输入框获得焦点时的样式
        :deep(.el-textarea__inner:focus) {
          border-color: var(--theme-color);
          box-shadow: 0 0 0 2px rgba(var(--theme-color-rgb), 0.1);
        }
      }

      .el-radio-group {
        display: flex;
        gap: 15px;

        .el-radio {
          margin-right: 0;
          transition: all 0.3s ease;
        }
      }

      .el-select {
        width: 100%;

        :deep(.el-input__wrapper) {
          border-radius: 25px;
          background-color: var(--dark-background-color);
        }
      }

      :deep(.el-form-item__content) {
        display: flex;
        gap: 10px;
      }

      // 修改按钮容器样式
      :deep(.el-form-item:last-child) {
        margin-bottom: 0;
        margin-top: 20px;

        .el-form-item__content {
          justify-content: flex-end;
          gap: 15px;
        }
      }

      button {
        width: 80px; // 设置固定宽度
        height: 36px; // 设置固定高度
        padding: 0; // 移除内边距
        flex: none; // 移除 flex 属性
        max-width: none; // 移除最大宽度限制
        background: linear-gradient(45deg, var(--theme-color), rgba(var(--theme-color-rgb), 0.8));
        color: white;
        border: none;
        font-size: 14px;
        border-radius: 18px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 3px 10px rgba(var(--theme-color-rgb), 0.2);

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 5px 15px rgba(var(--theme-color-rgb), 0.3);
        }

        &.cancel-btn {
          background: linear-gradient(45deg, var(--theme-blue-color), rgba(var(--theme-blue-color-rgb), 0.8));
          box-shadow: 0 3px 10px rgba(var(--theme-blue-color-rgb), 0.2);

          &:hover {
            box-shadow: 0 5px 15px rgba(var(--theme-blue-color-rgb), 0.3);
          }
        }
      }
    }
  }
}

.icon {
  width: 1.2em;
  height: 1.2em;
  fill: currentColor;
  overflow: hidden;
}
</style>
